package com.ruoyi.forum.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.forum.domain.vo.ForumReviewVo;
import com.ruoyi.forum.utils.SocialistCoreCheck;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.forum.domain.ForumReview;
import com.ruoyi.forum.service.IForumReviewService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论坛-评论Controller
 * 
 * @author ruoyi
 * @date 2024-02-12
 */
@RestController
@RequestMapping("/forum/review")
public class ForumReviewController extends BaseController
{
    @Autowired
    private IForumReviewService forumReviewService;

    @Autowired
    private SocialistCoreCheck socialistCoreCheck;

    /**
     * 查询论坛-评论列表
     */
    @PreAuthorize("@ss.hasPermi('forum:review:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForumReview forumReview)
    {
        startPage();
        if(forumReview.getDelFlag()==null){
            forumReview.setDelFlag("0");
        }
        List<ForumReview> list = forumReviewService.selectForumReviewList(forumReview);
        return getDataTable(list);
    }

    /**
     * 查询论坛-根据一个贴子id获取其所有的评论
     */
    @PreAuthorize("@ss.hasPermi('forum:review:listForPostId')")
    @GetMapping("/listForPostId")
    public TableDataInfo listForPostId(Long postId)
    {
//        startPage();
        List<ForumReviewVo> list = forumReviewService.selectForumReviewListForPostId(postId);
        return getDataTable(list);
    }

    /**
     * 查询某一天的评论发布量。
     */
    @GetMapping("/number/{date}")
    @Anonymous
    public Map<String, Object> getPostNumberForDay(@PathVariable Date date)
    {
        int i = forumReviewService.selectForumReviewByDate(date);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", HttpStatus.SUCCESS);
        map.put("msg","查询成功");
        HashMap<String , Object> data = new HashMap<>();
        data.put("date",date);
        data.put("size",i);
        map.put("data", data);
        return map;
    }

    /**
     * 导出论坛-评论列表
     */
    @PreAuthorize("@ss.hasPermi('forum:review:export')")
    @Log(title = "论坛-评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumReview forumReview)
    {
        List<ForumReview> list = forumReviewService.selectForumReviewList(forumReview);
        ExcelUtil<ForumReview> util = new ExcelUtil<ForumReview>(ForumReview.class);
        util.exportExcel(response, list, "论坛-评论数据");
    }

    /**
     * 获取论坛-评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('forum:review:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(forumReviewService.selectForumReviewById(id));
    }

    /**
     * 新增论坛-评论
     */
    @PreAuthorize("@ss.hasPermi('forum:review:add')")
    @Log(title = "论坛-评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumReview forumReview)
    {
        boolean check = socialistCoreCheck.checkContent(forumReview.getContent());
        if (!check){
            return new AjaxResult(HttpStatus.check,"发布内容有问题，请重新编辑更改，要符合社会主义核心价值观！");
        }
        forumReview.setCreateBy(getUsername());
        return toAjax(forumReviewService.insertForumReview(forumReview));
    }

    /**
     * 修改论坛-评论
     */
    @PreAuthorize("@ss.hasPermi('forum:review:edit')")
    @Log(title = "论坛-评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumReview forumReview)
    {
        forumReview.setUpdateBy(getUsername());
        return toAjax(forumReviewService.updateForumReview(forumReview));
    }

    /**
     * 删除论坛-评论
     */
    @PreAuthorize("@ss.hasPermi('forum:review:remove')")
    @Log(title = "论坛-评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumReviewService.deleteForumReviewByIds(ids));
    }
}
